package com.zx.sercurity;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ApplicationRisk implements RiskControlInterface<RiskResult.ApplicationRiskResult> {
    @Override
    public RiskResult.ApplicationRiskResult riskCheck(Context context) {
        String json = null;
        try {
            InputStream is = context.getAssets().open("application_list.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            return null;
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(json);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        List<String> riskPackageList = new ArrayList<>();
        try {
            JSONArray list = jsonObject.getJSONArray("list");
            for (int i = 0; i < list.length(); i++) {
                riskPackageList.add(list.getString(i));
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        List<String> installedPackage = new ArrayList<>();
        for (String s : riskPackageList) {
            if (checkApplicationPackage(context, s)) {
                installedPackage.add(s);
            }
        }
//        List<String> allInstalledPackages = getAllInstalledPackages(context);
//        StringBuilder stringBuilder = new StringBuilder();
//        for (String allInstalledPackage : allInstalledPackages) {
//            stringBuilder.append(allInstalledPackage).append("\n");
//        }
//        allInstalledPackages.retainAll(riskPackageList);
        if (installedPackage.isEmpty()) {
            return new RiskResult.ApplicationRiskResult(false);
        } else {
            return new RiskResult.ApplicationRiskResult(installedPackage);
        }
    }

    private boolean checkApplicationPackage(Context context,String targetPackage) {
        PackageManager packageManager = context.getPackageManager();
        try {
            packageManager.getPackageInfo(targetPackage, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    public static List<String> getAllInstalledPackages(Context context) {
        List<String> packageNames = new ArrayList<>();
        PackageManager packageManager = context.getPackageManager();

        // 获取所有已安装的应用（包括系统应用）
        List<PackageInfo> installedPackages = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);

        // 遍历应用列表，获取包名
        for (PackageInfo packageInfo : installedPackages) {
            packageNames.add(packageInfo.packageName);
        }

        return packageNames;
    }

}
